package br.com.farmabid.cadastro;

import java.util.List;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import br.com.farmabid.persistence.AbstractDAO;

@Named("farmaciaDAO")
public class FarmaciaDAO extends AbstractDAO {


	private static final long serialVersionUID = -196738644541630320L;
	
	public FarmaciaDAO(EntityManager manager) {
		this.manager = manager;
	}
	
	public FarmaciaDAO(){}
	
	public Farmacia recuperaFarmaciaPorCNPJ(String cnpj) {
		CriteriaBuilder qb = manager.getCriteriaBuilder();
		CriteriaQuery<Farmacia> cq = qb.createQuery(Farmacia.class);
		Root<Farmacia> tb = cq.from(Farmacia.class);
		cq.select(tb);
		cq.where(qb.equal(tb.get("cnpj"), cnpj));
		Query query = manager.createQuery(cq);
		return (Farmacia) recuperaRegistro(query);
	}
	
	public List<Farmacia> recuperaFarmaciaSemUsuario() {
		CriteriaBuilder qb = manager.getCriteriaBuilder();
		CriteriaQuery<Farmacia> cq = qb.createQuery(Farmacia.class);
		Root<Farmacia> tb = cq.from(Farmacia.class);
		cq.select(tb);
		 Predicate n1 = qb.isNull(tb.get("usuario"));
		cq.where(n1);
		Query query = manager.createQuery(cq);
		return (List<Farmacia>) recuperaLista(query);
	}
}
